package com.linq.cool.sdk.template;


import com.linq.cool.qqbot.myqq.utils.json.JsonUtils;
import com.linq.cool.sdk.config.ItpkRobotProperties;
import com.linq.cool.sdk.dto.ItpkReceiveQQMsgDto;
import com.mzlion.easyokhttp.HttpClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: yqlin
 * @date: 2021/7/4 21:50
 * @description:
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ItpkHttpRequestTemplate {
    private final ItpkRobotProperties itpkRobotProperties;

    /**
     * http://i.itpk.cn/api.php?question=%E4%BD%A0%E5%A4%A7%E7%88%B7&api_key=3609a713e14c424e80cae583f3df3552&api_secret=fivcbz34uq3k
     */
    public String doRequest(ItpkReceiveQQMsgDto dto) {
        final String url = itpkRobotProperties.getBaseUri();
        Map<String, String> queryParams = new HashMap<>(8);
        queryParams.put("question", dto.getMsg());
        queryParams.put("api_key", itpkRobotProperties.getApiKey());
        queryParams.put("api_secret", itpkRobotProperties.getSecretKey());
        log.info("\n\n请求: " + url + "， 请求方式: GET, 请求参数: " + queryParams + "\n\n");
        String response = HttpClient.get(url)
                .queryString(queryParams)
                .asString();
        log.info("返回数据:\n {}\n", JsonUtils.printJson(response));
        return response;
    }

}
